#include "familydialog.h"
#include "ui_familydialog.h"
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<QSqlDatabase>
#include<QCompleter>
#include"qualifydocu.h"
#include"sqlconnect.h"
familyDialog::familyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::familyDialog)
{
    ui->setupUi(this);
    //关系
    QStringList strings;
    QSqlQuery query("SELECT Relation FROM RelationCode");
    while (query.next()) {
    QString relation = query.value(0).toString();
    strings.append(relation);
    }
    QCompleter *completer = new QCompleter(strings, this);
    ui->relationBox->clear();
    ui->relationBox->addItems(strings);
    ui->relationBox->setEditable(true);
    ui->relationBox->setCompleter(completer);
    //政治面貌
    QStringList strings2;
    QSqlQuery query2("SELECT Politic FROM PoliticCode");
    while (query2.next()) {
    QString politic = query2.value(0).toString();
    strings2.append(politic);
    }
    completer = new QCompleter(strings2, this);
    ui->politicBox->clear();
    ui->politicBox->addItems(strings2);
    ui->politicBox->setEditable(true);
    ui->politicBox->setCompleter(completer);
    connect(ui->cancel,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->confirm,SIGNAL(clicked(bool)),this,SLOT(react()));
    connect(ui->confirm,SIGNAL(clicked(bool)),parent,SLOT(showFamilyInfo()));
}

familyDialog::~familyDialog()
{
    delete ui;
}
void familyDialog::react(){
    QString name=ui->nameEdit->text();
    QString birth=ui->birthday->text();
    //关系代码
    QString relation=ui->relationBox->currentText();
    //qDebug()<<relation;
    QString relationid;
    QSqlQuery query;
    query.exec("SELECT RelationID FROM RelationCode where Relation='"+relation+"'");
    while (query.next()) {
    relationid = query.value(0).toString();
    }
    //政治面貌
    QString politic=ui->politicBox->currentText();
    QString politicid;
    query.exec("SELECT PoliticID FROM PoliticCode where Politic='"+politic+"'");
        while (query.next()) {
        politicid = query.value(0).toString();
        }
    QString unit=ui->unitEdit->text();
    QString work=ui->postEdit->text();
    QString num=ui->numEdit->text();
    QString note=ui->noteEdit->text();
    if(name.count()==0||num.count()==0){
        QMessageBox::information(this,"提示信息","姓名或证件号不得为空！");
        return;
        }
    //sqlconnect(db);
    //QSqlQuery query;
    QSqlQuery qu("select * from  `Family` where FamilyNum='"+num+"' and IDNum='"+ID+"'");
    if(qu.next())
    {
        QMessageBox::information(this,"提示信息","该亲属已存在，不可重复添加！");
        return;
    }


    QString sql="INSERT INTO `Family`(Name,BirthTime,Relation,Politic,UnitName,Profession,FamilyNum,FileRecRmk,IDNum)VALUES('"+name+"','"+birth+"','"+relationid+"','"+politicid+"','"+unit+"','"+work+"','"+num+"','"+note+"','"+ID+"')";
    bool ok=query.exec(sql);
    qDebug()<<ok;
    if(ok){
        QMessageBox::information(this,"提示信息","成员成功插入!");
    }
    this->close();
}

void familyDialog::reciveID(QString s){
    ID=s;
}
